// pages/home-profile/index.js
const db = wx.cloud.database()
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import {userStore} from '../../store/index'
Page({
  data: {
    userInfoList:[],
    id:"",
    isLogin:"false",
    username:"",
    haveCollection:false,
    collectionList:[],
    collectionShow:false,
    collectionWord:"",
    collectionIndex:0,
    showEdit:true
  },

  onLoad: async function (options) {
    this.userLogin()
  },
  loginout(){
    Dialog.confirm({
      message: '确认要退出登录吗？',
    })
    .then(()=>{
      db.collection('user').doc(this.data.id).update({
        data:{
          isLogin:"false"
        },
      })   
        this.userInfoChange()
        this.setData({ collectionList:[] })
    })
    .catch(()=>{
      return
    })
    
    
  },
  //退出清空数据
  userInfoChange(){
        this.setData({
          id:"",
          userInfoList:[]
        })   
        userStore.dispatch("clearUserInfo")
  },
  profile(){
    wx.navigateTo({
      url: '/pages/profile-data/index',
    })
  },
  login(){
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  async userLogin(){
    userStore.onState("isLogin",(value)=>{
      this.setData({
        isLogin:value
      })
    })
    userStore.onState("id",(value)=>{
      this.setData({
        id:value
      })
    })
    userStore.onState("username",(value)=>{
      this.setData({username:value})
    })
    if(this.data.id !== ""){
      db.collection('user').doc(this.data.id).get().then(res=>{
              
        this.setData({
          userInfoList:res
        })     
      })
    }
    if(this.data.isLogin == 'true'){    
      //是否有歌单
     await db.collection('collectionMenu').where({
        userName:this.data.username
      }).get().then(res =>{
        if(res.data.length == 0 && this.data.isLogin =='true' ){
          this.setData({haveCollection:false})
        }
        else if(res.data.length != 0 && this.data.isLogin == 'true'){
            
            this.setData({collectionList:res.data})
            this.setData({haveCollection:true})
            
        }
      })

    }
  },

  // 点击歌单
  handleSongitemClick(event){
    const index = event.currentTarget.dataset.index
    var id = this.data.collectionList[index]._id
    wx.navigateTo({
      url: '/pages/playlist-detail/index?id=' + id,
    })
  },

  //点击更多按钮
  handleMoreClick:function(event){
    this.userLogin()
    var index = event.currentTarget.dataset.index
    this.setData({collectionIndex:index})
    const collectionWord = this.data.collectionList[index].name
    console.log(index);
    var id = this.data.collectionList[index]._id
    console.log(id);

    if(this.data.collectionList[index].id){
      console.log('有值的');
      this.setData({showEdit:false})
    }
    else if(!this.data.collectionList[index].id){
      this.setData({showEdit:true})
    }
  

    this.setData({collectionWord:collectionWord})
    this.setData({collectionShow:true})
  },

  //删除按钮
  handleDeleteClick:function(){
    var index = this.data.collectionIndex
    var id = this.data.collectionList[index]._id
  
    Dialog.confirm({
      message: '确定要删除该歌单吗？',
    })
    .then(()=>{
      db.collection('collectionMenu').doc(id).remove()
      .then(res=>{
        console.log(res);
        this.setData({collectionShow:false})
        Toast('删除成功');
        this.userLogin()
      })
    })
    .catch(()=>{
      return
    })
  },

  //编辑按钮
  handleEditClick:function(){
    var index = this.data.collectionIndex
    var id = this.data.collectionList[index]._id
    this.setData({collectionShow:false})
    wx.navigateTo({
      url: '/pages/edit-song-menu/index?id='+ id,
    })
  },

  onCollectionClose:function(){
    this.setData({collectionShow:false})
  },

  onShow: async function(){
   await this.userLogin()
  }
})